import { PublicInterface } from '@/interface/public';

/**
 * @name 操作对象
 */

/**
 *
 * @param obj 原对象
 * @param str 需要写入的字段(以逗号分割)
 * @returns 新的对象，保留指定原值对象
 */
export const setNewObjValue = (obj: PublicInterface.UnknownObjProps, str: string) => {
  const arr = str.split(',');
  let newObj: any = {};
  for (let key of arr) {
    // 判断是否需要转换字段名称
    if (!key.includes(':')) {
      newObj[key] = obj[key];
    } else {
      const v = key.split(':');
      newObj[v[0]] = obj[v[1]];
    }
  }
  return newObj;
};

/**
 * @description 将对象内的对象拆分出来并添加指定前缀作为区分
 */
export function splitParamsObj(obj: PublicInterface.UnknownObjProps, flag: string = 'm_') {
  if (!obj) return {};
  const newObj = {};
  Object.keys(obj).forEach(key => {
    if (isValidKey(key, obj)) {
      newObj[flag + key] = obj[key];
    }
  });
  return newObj;
}
